{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8935c227",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "#第一问\n",
    "import pandas as pd\n",
    "df = pd.read_csv('F:\\\\gaze.csv')\n",
    "csv_data = df[df['confidence']>=0.9]\n",
    "print(len(csv_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ea7877db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
      "21     1970-01-01 00:00:00.000088150            2    0.915197    0.493612   \n",
      "22     1970-01-01 00:00:00.000088150            2    0.915197    0.493561   \n",
      "23     1970-01-01 00:00:00.000088150            2    0.915197    0.493570   \n",
      "39     1970-01-01 00:00:00.000088150            3    0.922197    0.492592   \n",
      "40     1970-01-01 00:00:00.000088150            3    0.922197    0.492635   \n",
      "...                              ...          ...         ...         ...   \n",
      "125259 1970-01-01 00:00:00.000088443         8788    0.995336    0.407831   \n",
      "125260 1970-01-01 00:00:00.000088443         8788    0.948733    0.407552   \n",
      "125261 1970-01-01 00:00:00.000088443         8788    0.945284    0.407467   \n",
      "125262 1970-01-01 00:00:00.000088443         8788    0.945284    0.407160   \n",
      "125263 1970-01-01 00:00:00.000088443         8788    0.945284    0.407637   \n",
      "\n",
      "        norm_pos_y                           base_data  gaze_point_3d_x  \\\n",
      "21        0.398950       88150.827729-0 88150.823548-1         2.412810   \n",
      "22        0.399305       88150.827729-0 88150.827729-1         2.429837   \n",
      "23        0.399542  88150.827729-0 88150.83159799999-1         2.427118   \n",
      "39        0.398705  88150.867514-0 88150.86451599999-1         2.827911   \n",
      "40        0.398814       88150.867514-0 88150.871726-1         2.813774   \n",
      "...            ...                                 ...              ...   \n",
      "125259    0.413470        88443.69561-0 88443.695591-1       327.648915   \n",
      "125260    0.414731   88443.69561-0 88443.69953099999-1       311.054477   \n",
      "125261    0.414705  88443.703629-0 88443.69953099999-1       313.566509   \n",
      "125262    0.415340       88443.703629-0 88443.703587-1       297.019831   \n",
      "125263    0.413674  88443.703629-0 88443.70751899999-1       323.629974   \n",
      "\n",
      "        gaze_point_3d_y  gaze_point_3d_z  eye_center0_3d_x  ...  \\\n",
      "21           -21.468145      -295.071057         20.000000  ...   \n",
      "22           -21.374413      -294.819200         20.000000  ...   \n",
      "23           -21.328193      -294.874516         20.000000  ...   \n",
      "39           -21.750938      -298.233245         20.000000  ...   \n",
      "40           -21.744854      -298.471253         20.000000  ...   \n",
      "...                 ...              ...               ...  ...   \n",
      "125259      -173.027524     -2777.243340         19.888755  ...   \n",
      "125260      -161.381388     -2628.637408         19.888755  ...   \n",
      "125261      -162.585330     -2647.420719         19.888755  ...   \n",
      "125262      -152.352987     -2499.428189         19.888755  ...   \n",
      "125263      -170.142461     -2737.404466         19.888755  ...   \n",
      "\n",
      "        eye_center0_3d_z  gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  \\\n",
      "21            -20.000000        0.062883        0.025100        0.997705   \n",
      "22            -20.000000        0.062883        0.025100        0.997705   \n",
      "23            -20.000000        0.062883        0.025100        0.997705   \n",
      "39            -20.000000        0.060717        0.025220        0.997836   \n",
      "40            -20.000000        0.060717        0.025220        0.997836   \n",
      "...                  ...             ...             ...             ...   \n",
      "125259        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125260        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125261        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125262        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125263        -20.707271       -0.111009        0.041794        0.992940   \n",
      "\n",
      "        eye_center1_3d_x  eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  \\\n",
      "21            -39.934928         14.997919        -20.075283       -0.150041   \n",
      "22            -39.934928         14.997919        -20.075283       -0.150243   \n",
      "23            -39.934928         14.997919        -20.075283       -0.150210   \n",
      "39            -39.934928         14.997919        -20.075283       -0.149814   \n",
      "40            -39.934928         14.997919        -20.075283       -0.149645   \n",
      "...                  ...               ...               ...             ...   \n",
      "125259        -37.372594         14.763454        -23.294339       -0.130868   \n",
      "125260        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125261        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125262        -37.372594         14.763454        -23.294339       -0.133311   \n",
      "125263        -37.372594         14.763454        -23.294339       -0.131320   \n",
      "\n",
      "        gaze_normal1_y  gaze_normal1_z  \n",
      "21            0.234536        0.960458  \n",
      "22            0.234115        0.960530  \n",
      "23            0.233747        0.960624  \n",
      "39            0.233510        0.960744  \n",
      "40            0.233258        0.960831  \n",
      "...                ...             ...  \n",
      "125259        0.093021        0.987026  \n",
      "125260        0.091860        0.986979  \n",
      "125261        0.091860        0.986979  \n",
      "125262        0.091656        0.986827  \n",
      "125263        0.092941        0.986974  \n",
      "\n",
      "[86185 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "#第二问\n",
    "import numpy as np\n",
    "norm_x = csv_data['norm_pos_x']\n",
    "norm_y = csv_data['norm_pos_y']\n",
    "mean_x = np.mean(norm_x)\n",
    "mean_y = np.mean(norm_y)\n",
    "std_x = np.std(norm_x)\n",
    "std_y = np.std(norm_y)\n",
    "x1 = mean_x - 3 * std_x\n",
    "x2 = mean_x + 3 * std_x\n",
    "y1 = mean_y - 3 * std_y\n",
    "y2 = mean_y + 3 * std_y\n",
    "csv_data1 = csv_data[(csv_data['norm_pos_x'] > x1) & (csv_data['norm_pos_x'] < x2) & (csv_data['norm_pos_y'] > y1) & (csv_data['norm_pos_y'] < y2)]\n",
    "print(csv_data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "0f981630",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
      "21     1970-01-01 00:00:00.000088150            2    0.915197    0.493612   \n",
      "22     1970-01-01 00:00:00.000088150            2    0.915197    0.493561   \n",
      "23     1970-01-01 00:00:00.000088150            2    0.915197    0.493570   \n",
      "39     1970-01-01 00:00:00.000088150            3    0.922197    0.492592   \n",
      "40     1970-01-01 00:00:00.000088150            3    0.922197    0.492635   \n",
      "...                              ...          ...         ...         ...   \n",
      "125259 1970-01-01 00:00:00.000088443         8788    0.995336    0.407831   \n",
      "125260 1970-01-01 00:00:00.000088443         8788    0.948733    0.407552   \n",
      "125261 1970-01-01 00:00:00.000088443         8788    0.945284    0.407467   \n",
      "125262 1970-01-01 00:00:00.000088443         8788    0.945284    0.407160   \n",
      "125263 1970-01-01 00:00:00.000088443         8788    0.945284    0.407637   \n",
      "\n",
      "        norm_pos_y                           base_data  gaze_point_3d_x  \\\n",
      "21        0.398950       88150.827729-0 88150.823548-1         2.412810   \n",
      "22        0.399305       88150.827729-0 88150.827729-1         2.429837   \n",
      "23        0.399542  88150.827729-0 88150.83159799999-1         2.427118   \n",
      "39        0.398705  88150.867514-0 88150.86451599999-1         2.827911   \n",
      "40        0.398814       88150.867514-0 88150.871726-1         2.813774   \n",
      "...            ...                                 ...              ...   \n",
      "125259    0.413470        88443.69561-0 88443.695591-1       327.648915   \n",
      "125260    0.414731   88443.69561-0 88443.69953099999-1       311.054477   \n",
      "125261    0.414705  88443.703629-0 88443.69953099999-1       313.566509   \n",
      "125262    0.415340       88443.703629-0 88443.703587-1       297.019831   \n",
      "125263    0.413674  88443.703629-0 88443.70751899999-1       323.629974   \n",
      "\n",
      "        gaze_point_3d_y  gaze_point_3d_z  eye_center0_3d_x  ...  \\\n",
      "21           -21.468145      -295.071057         20.000000  ...   \n",
      "22           -21.374413      -294.819200         20.000000  ...   \n",
      "23           -21.328193      -294.874516         20.000000  ...   \n",
      "39           -21.750938      -298.233245         20.000000  ...   \n",
      "40           -21.744854      -298.471253         20.000000  ...   \n",
      "...                 ...              ...               ...  ...   \n",
      "125259      -173.027524     -2777.243340         19.888755  ...   \n",
      "125260      -161.381388     -2628.637408         19.888755  ...   \n",
      "125261      -162.585330     -2647.420719         19.888755  ...   \n",
      "125262      -152.352987     -2499.428189         19.888755  ...   \n",
      "125263      -170.142461     -2737.404466         19.888755  ...   \n",
      "\n",
      "        eye_center0_3d_z  gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  \\\n",
      "21            -20.000000        0.062883        0.025100        0.997705   \n",
      "22            -20.000000        0.062883        0.025100        0.997705   \n",
      "23            -20.000000        0.062883        0.025100        0.997705   \n",
      "39            -20.000000        0.060717        0.025220        0.997836   \n",
      "40            -20.000000        0.060717        0.025220        0.997836   \n",
      "...                  ...             ...             ...             ...   \n",
      "125259        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125260        -20.707271       -0.110854        0.041843        0.992955   \n",
      "125261        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125262        -20.707271       -0.111009        0.041794        0.992940   \n",
      "125263        -20.707271       -0.111009        0.041794        0.992940   \n",
      "\n",
      "        eye_center1_3d_x  eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  \\\n",
      "21            -39.934928         14.997919        -20.075283       -0.150041   \n",
      "22            -39.934928         14.997919        -20.075283       -0.150243   \n",
      "23            -39.934928         14.997919        -20.075283       -0.150210   \n",
      "39            -39.934928         14.997919        -20.075283       -0.149814   \n",
      "40            -39.934928         14.997919        -20.075283       -0.149645   \n",
      "...                  ...               ...               ...             ...   \n",
      "125259        -37.372594         14.763454        -23.294339       -0.130868   \n",
      "125260        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125261        -37.372594         14.763454        -23.294339       -0.132037   \n",
      "125262        -37.372594         14.763454        -23.294339       -0.133311   \n",
      "125263        -37.372594         14.763454        -23.294339       -0.131320   \n",
      "\n",
      "        gaze_normal1_y  gaze_normal1_z  \n",
      "21            0.234536        0.960458  \n",
      "22            0.234115        0.960530  \n",
      "23            0.233747        0.960624  \n",
      "39            0.233510        0.960744  \n",
      "40            0.233258        0.960831  \n",
      "...                ...             ...  \n",
      "125259        0.093021        0.987026  \n",
      "125260        0.091860        0.986979  \n",
      "125261        0.091860        0.986979  \n",
      "125262        0.091656        0.986827  \n",
      "125263        0.092941        0.986974  \n",
      "\n",
      "[87442 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "#第三问\n",
    "csv_data1=csv_data.copy()\n",
    "csv_data1.loc[:,'gaze_timestamp'] = pd.to_datetime(csv_data1.loc[:,'gaze_timestamp'])\n",
    "print(csv_data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "93533e83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.62610766083765\n"
     ]
    }
   ],
   "source": [
    "#第四问\n",
    "num = len(df)\n",
    "t1 = df['gaze_timestamp'][0]\n",
    "t2 = df['gaze_timestamp'][125263]\n",
    "sanmple_rates = num / (t2 - t1)\n",
    "print(sample_rates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "9c88eafd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88150.77678, 88150.787485, 88150.79840649999, 88150.80726949999, 88150.818051, 88150.827729, 88150.83957499999, 88150.84950499999, 88150.85956099999, 88150.86962, 88150.87958449998, 88150.88999649999, 88150.8995115, 88150.90939799999, 88150.91942349999, 88150.9275245, 88150.93855949999, 88150.947523, 88150.95950249999, 88150.96950749999, 88150.97952299999]\n"
     ]
    }
   ],
   "source": [
    "temp = df['gaze_timestamp'][0]\n",
    "resample = []\n",
    "resample.append(df['gaze_timestamp'][0])\n",
    "i = 1\n",
    "fre = 0.01\n",
    "diff = flag = 1\n",
    "while len(resample) < 21 :\n",
    "    diff = abs(float(df['gaze_timestamp'][i] - temp - fre))\n",
    "    if diff >= flag:\n",
    "        diff = 1\n",
    "        resample.append(df['gaze_timestamp'][i-1])\n",
    "        temp = df['gaze_timestamp'][i-1]\n",
    "    i += 1\n",
    "    flag = diff\n",
    "\n",
    "print(resample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8057a5ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
